package com.control;

import java.io.IOException;
import java.net.MalformedURLException;
import java.util.ArrayList;

import jdbm.htree.HTree;

import org.htmlparser.Parser;
import org.htmlparser.util.ParserException;

import com.access.Spider;
import com.data.JDoc;

public class DocManager {

	private ArrayList<JDoc> docs = new ArrayList<JDoc>();
	private ArrayList<String> urls;

	private Spider spider = new Spider();

	public ArrayList<String> getUrls() {
		return urls;
	}

	public void initializeDocs(String root, int limit) {
		urls = new ArrayList<String>();
		ArrayList<String> queue = new ArrayList<String>();
		urls.add(root);
		queue.add(root);
		try {
			searchUrl(queue, null, limit);
		} catch (ParserException | IOException e) {
			e.printStackTrace();
		}

	}

	private void searchUrl(ArrayList<String> queue, String parent, int limit)
			throws ParserException, MalformedURLException, IOException {

		ArrayList<String> childLinks;
		JDoc doc;
		String currentNode;
		if (queue != null && queue.size() > 0) {
			currentNode = queue.remove(0);
			childLinks = spider.extractChildLinks(currentNode);
			doc = new JDoc(spider.extractTitle(currentNode), currentNode,
					parent, spider.extractLastModifiedDate(currentNode),
					childLinks);
			docs.add(doc);
		} else {
			return;
		}
		for (String child : childLinks) {

			if (!urls.contains(child)) {
				if (urls.size() >= limit) {
					break;
				}
				try {
					new Parser().setURL(child);
					urls.add(child);
					queue.add(child);
				} catch (ParserException e) {

				}
			}
		}
		searchUrl(queue, currentNode, limit);
	}

	public void buildDocIndex(ArrayList<Integer> docSize,
			ArrayList<Integer> max_tf,
			ArrayList<ArrayList<String>> wordsForDocs, JDBManager dbManager) {

		for (int id = 0; id < urls.size(); id++) {
			JDoc doc = docs.get(id);
			doc.setId(id);
			doc.setSize(docSize.get(id));
			doc.setMaxTermFreq(max_tf.get(id));
			doc.setWords(wordsForDocs.get(id));
		}

		HTree validator, documents;
		try {
			validator = dbManager.loadHashTable("validator");
			dbManager.buildValidator(urls, validator);

			documents = dbManager.loadHashTable("documents");
			dbManager.buildHashTable(docs, documents);

		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
}
